package com.itheima.controller;

import com.itheima.service.DeptService;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    //private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    /*
    查询部门
     */
    @GetMapping
    public Result list() {

        List<Dept> deptList = deptService.findAll();
        log.info("查询到部门：{}", deptList);
        return Result.success(deptList);
    }

    /*
    根据id删除部门
     */
    @DeleteMapping

    public Result delete(Integer id) {

        deptService.delete(id);
        log.info("删除部门：{}", id);
        return Result.success();
    }

    /**
     * 添加部门
     */
    @PostMapping
    public Result add(@RequestBody Dept dept) {

        deptService.add(dept);
        log.info("添加部门：{}", dept);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {

        Dept dept = deptService.findById(id);
        log.info("查询到部门：{}", dept);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门：{}", dept);
        deptService.update(dept);
        log.info("修改部门成功");
        return Result.success();
    }
}
